package com.pointi.emoBasedataM ;

import java.util.concurrent.ConcurrentLinkedQueue;

import com.pointi.emoTools.DefineParam;

public class emoBasedata
{
	public static void main (String [] args) throws Exception
	{
		int checkInterval = 1000 ;
		int schedulerLimit = 1 ;
		int schedulerCount = 0 ;
		
		// ____________________________________________________________________________________________________
		// Read Configuration Data

		String str_conffile = "emoBasedataM.conf";
		ConfigurationData conf_data = new ConfigurationData();
		conf_data.InitializeData();
		try
		{
			conf_data = conf_data.ReadConfXML(str_conffile);
			conf_data.WriteLogConfigurationData(conf_data);
		}
		catch (Exception e)
		{
			System.out.println("Can't Read XML File!! - "+str_conffile);
			e.printStackTrace();
		}
		checkInterval = Integer.valueOf(conf_data.s_check_interval) ;

		// ____________________________________________________________________________________________________
		// Get Licensed Channels

		int n_target_size = conf_data.GetLicenseChannel(conf_data.s_server_key);
		System.out.println("Licensed Channels - "+conf_data.s_server_chtype+ " : " + n_target_size);

		if (n_target_size == 0)
		{
			System.out.println("Fail Check License!");
		}
		else
		{
			// ____________________________________________________________________________________________________
			// JDBC | Connect Database

			JDBC jdbc = new JDBC() ;
			jdbc.SetDatabaseParam(conf_data) ;
			DefineParam define = new DefineParam();
			int nRet = define.DB_CONNECT_FAIL ;
			while (true) 
			{
				nRet = jdbc.fnConnectDB();
				if (nRet == define.DB_CONNECT_SUCCESS) break;
				System.out.println("Record DB Connect - "+nRet);
				Thread.sleep(1000);
			}
			jdbc.fnPrepareDatabase() ;

			// ____________________________________________________________________________________________________
			// WorkThread
			
			ConcurrentLinkedQueue<ThreadScheduler> workScheduler = new ConcurrentLinkedQueue<ThreadScheduler>() ;
			boolean loopHandler = true ;
			while (loopHandler)
			{
				if (workScheduler.isEmpty())
				{
					workScheduler = new ConcurrentLinkedQueue<ThreadScheduler>() ;
				}
				schedulerCount = workScheduler.size() ;
				if (schedulerCount < schedulerLimit)
				{
					new ThreadScheduler(workScheduler).start(conf_data, jdbc) ;
				}
				Thread.sleep(checkInterval) ;
			}
		}
	}
}
